//
//  BKCusTextView.m
//  bestkeep
//
//  Created by windCould on 16/11/10.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKCusTextView.h"
#import "BKMyCommon.h"
@interface BKCusTextView ()<UITextViewDelegate>

@property (nonatomic,strong)UILabel * placeHolderLabel;
@property (nonatomic,strong)UILabel * recordTextLengthLab;


@end


@implementation BKCusTextView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpViews];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textContentStringChanged:) name:UITextViewTextDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textContentEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
        self.delegate = self;
    }
    
    return  self;
}

-(void)setUpViews{
    
    self.font = UIFontNormal;
    
    _placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _placeHolderLabel.textColor = HexColor(0xB2B2B2);
    _placeHolderLabel.font = [UIFont systemFontOfSize:13];
    _placeHolderLabel.numberOfLines = 0;
    
    _recordTextLengthLab = [[UILabel alloc]initWithFrame:CGRectZero];
    _recordTextLengthLab.textColor = [UIColor redColor];
    _recordTextLengthLab.text = @"0/100";
    _recordTextLengthLab.font = UIFontNormal;
    _recordTextLengthLab.textAlignment = NSTextAlignmentRight;
    _recordTextLengthLab.hidden = YES;
    [self addSubview:_placeHolderLabel];
   
    [self addSubview:_recordTextLengthLab];
    
    [self layoutViews];

}


-(void)layoutViews{
    
    
    [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.mas_left).offset(5);
        make.top.equalTo(self.mas_top).offset(8);
        make.width.equalTo(@(SCREENWIDTH -10));
    }];
    
    [_recordTextLengthLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mas_left).offset(SCREENWIDTH-95-ApplicationBackGauge*2-8);
        make.top.mas_equalTo(self.mas_top).offset(140);
        make.size.mas_equalTo(CGSizeMake(95, 20));
    }];
    
}

#pragma mark =========================内容输入通知===================================

-(void)textContentStringChanged:(NSNotification *)notice
{
    if (self.text.length == 0 || [self.text isEqualToString:@""]) {
        
        self.placeHolderLabel.hidden = NO;
    } else {
    
        self.placeHolderLabel.hidden = YES;
    }
    
    NSInteger textLength = self.text.length;
    NSRange range = {0,textLength};
    [self textView:self shouldChangeTextInRange:range replacementText:@""];
    
    if (self.text.length >100 ) {
        
        self.text = [self.text substringToIndex:100];
    }
    _recordTextLengthLab.text = [NSString stringWithFormat:@"%lu/%d",(unsigned long)self.text.length,100];
}

-(void)textContentEndEditing:(NSNotification *)notice
{
    
    if ([self.textDelegate respondsToSelector:@selector(customTextview: endEditingWithContent:)]) {
        
        [self.textDelegate customTextview:self endEditingWithContent:self.text];
    }
    
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([textView.text isEqualToString:@""] && range.length > 0) {
        //删除字符肯定是安全的
        return YES;
    }
    else {
        
        if (textView.text.length - range.length + text.length > 100) {
            
            return NO;
        }
        else {
            
            return YES;
        }
    }
    
    
    
}




-(void)setPlaceHolderString:(NSString *)placeHolderString
{
    _placeHolderLabel.text = placeHolderString;
}

-(void)setContentFont:(UIFont *)contentFont
{
    _placeHolderLabel.font = contentFont;
}

-(void)setContentColor:(UIColor *)contentColor
{

    _placeHolderLabel.textColor = contentColor;
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:nil];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
}





@end
